package com.yunji.web.system;

import com.yunji.common.annotation.Log;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.model.domain.SysDictTypeDO;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.system.context.SystemContext;
import com.yunji.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 字典类型信息
 *
 * @author ruoyi
 */
@Api(tags = "字典类型信息")
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {
    @Autowired
    private ISysDictTypeService dictTypeService;

    @ApiOperation("字典类型列表")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public Response<List<SysDictTypeDO>> list(SysDictTypeDO dictType) {
        List<SysDictTypeDO> list = dictTypeService.selectDictTypeList(dictType);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("字典类型导出")
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:dict:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictTypeDO dictType) {
        List<SysDictTypeDO> list = dictTypeService.selectDictTypeList(dictType);
        ExcelUtil<SysDictTypeDO> util = new ExcelUtil<>(SysDictTypeDO.class);
        util.exportExcel(response, list, "字典类型");
    }

    /**
     * 查询字典类型详细
     */
    @ApiOperation("查询字典类型详细")
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    public Response<SysDictTypeDO> getInfo(@PathVariable Long dictId) {
        return ResponseHelper.success(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @ApiOperation("新增字典类型")
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public Response<Long> add(@Validated @RequestBody SysDictTypeDO dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return ResponseHelper.failure("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(SystemContext.getUsername());
        Assert.isTrue(dictTypeService.insertDictType(dict) > 0, "新增失败");
        return ResponseHelper.success(dict.getDictId());
    }

    /**
     * 修改字典类型
     */
    @ApiOperation("修改字典类型")
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public Response<String> edit(@Validated @RequestBody SysDictTypeDO dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return ResponseHelper.failure("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(SystemContext.getUsername());
        Assert.isTrue(dictTypeService.updateDictType(dict) > 0, "修改失败");
        return ResponseHelper.success();
    }

    /**
     * 删除字典类型
     */
    @ApiOperation("删除字典类型")
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public Response<String> remove(@PathVariable Long[] dictIds) {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return ResponseHelper.success();
    }

    /**
     * 刷新字典缓存
     */
    @ApiOperation("刷新字典缓存")
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public Response<String> refreshCache() {
        dictTypeService.resetDictCache();
        return ResponseHelper.success();
    }

    /**
     * 获取字典选择框列表
     */
    @ApiOperation("获取字典选择框列表")
    @GetMapping("/optionselect")
    public Response<List<SysDictTypeDO>> optionselect() {
        List<SysDictTypeDO> dictTypes = dictTypeService.selectDictTypeAll();
        return ResponseHelper.success(dictTypes);
    }
}
